import { assign } from 'lodash'
import config from '@/config'
import {
  fetchFBChartData,
  fetchUserParams,
  fetchRLChartData,
  fetchHBarCharData,
  fetHPieChartData,
  fetchMeterDetail,
  fetchDiagReport,
  saveParams
} from '@/api/meterManagedistribution'

const defaultState = () => {
  return {
    defaultDate: config.nowDate,
    chartType: 'fb',
    currentMeterId: undefined,
    fbChartList: [],
    rlChartList: [],
    hBarChartList: [],
    hPieChartList: [],
    meterDetail: null,
    diagReport: {},
    collapseIcon: false,
    qsScore: 0,
    ycsjScore: 0
  }
}

export default {
  namespaced: true,
  name: 'meterManageDestribution',
  state: defaultState(),
  mutations: {
    REST_STATE(state) {
      state = assign(state, defaultState())
    },
    SET_FB_CHART_LIST(state, data) {
      state.fbChartList = data
    },
    SET_RL_CHART_LIST(state, data) {
      state.rlChartList = data
    },
    SET_HBAR_CHART_LIST(state, data) {
      state.hBarChartList = data
    },
    SET_HPIE_CHART_LIST(state, data) {
      state.hPieChartList = data
    },
    SET_CHART_TYPE(state, type) {
      state.chartType = type
    },
    SET_CURRENT_METER_ID(state, id) {
      state.currentMeterId = id
    },
    SET_METER_DETAIL(state, data) {
      state.meterDetail = data
    },
    SET_DIAGREPORT(state, data) {
      state.diagReport = data
    },
    SET_COLLAPSE_ICON(state, flag) {
      state.collapseIcon = flag
    },
    SET_QS_SCORE(state, num) {
      state.qsScore = num
    },
    SET_YCSJ_SCORE(state, num) {
      state.ycsjScore = num
    }
  },
  actions: {
    clearStore(ctx) {
      ctx.commit('REST_STATE')
    },
    async loadFBChartData(ctx, params) {
      const { code, date } = await fetchFBChartData(params)
      if (code === 200) {
        ctx.commit('SET_FB_CHART_LIST', date)
      }
    },
    async loadUserParams(ctx, params) {
      const _ret = await fetchUserParams(params)
      return _ret
    },
    async loadRLChartData(ctx, params) {
      const { code, date } = await fetchRLChartData(params)
      if (code === 200) {
        ctx.commit('SET_RL_CHART_LIST', date)
      }
    },
    async loadHBarCharData(ctx, params) {
      const { code, date } = await fetchHBarCharData(params)
      if (code === 200) {
        ctx.commit('SET_HBAR_CHART_LIST', date)
      }
    },
    async loadHPieCharList(ctx, params) {
      const { code, date } = await fetHPieChartData(params)
      if (code === 200) {
        ctx.commit('SET_HPIE_CHART_LIST', date)
      }
    },
    async loadMeterDetail(ctx) {
      const { code, date } = await fetchMeterDetail(ctx.state.currentMeterId)
      if (code === 200) {
        ctx.commit('SET_METER_DETAIL', date)
      }
    },
    async loadDiagReport(ctx, params) {
      const { code, date } = await fetchDiagReport(params)
      if (code === 200) {
        ctx.commit('SET_DIAGREPORT', date)
      }
    },
    changeCurrentMeterId(ctx, id) {
      ctx.commit('SET_CURRENT_METER_ID', id)
    },
    changeChartType(ctx, type) {
      ctx.commit('SET_CHART_TYPE', type)
    },
    changeCollapseIcon(ctx, flag) {
      ctx.commit('SET_COLLAPSE_ICON', flag)
    },
    changeHPieChartData(ctx, data) {
      ctx.commit('SET_HPIE_CHART_LIST', data)
    },
    changeMeterDetail(ctx, data) {
      ctx.commit('SET_METER_DETAIL', data)
    },
    changeQsScore(ctx, num) {
      ctx.commit('SET_QS_SCORE', num)
    },
    changeYcsjScore(ctx, num) {
      ctx.commit('SET_YCSJ_SCORE', num)
    },
    async loadSaveParams(ctx, params) {
      const _ret = await saveParams(params)
      return _ret
    }
  }
}
